package org.smr.ministore.service.impl;

import java.util.List;
import java.util.Objects;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.blade.msg.ErrorCode;
import org.blade.utils.IdGenUtils;
import org.blade.utils.Pager;
import org.blade.service.base.AbstractService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.smr.ministore.dao.GoodsTypeMapper;
import org.smr.ministore.dao.GoodsTypeOrganMapper;
import org.smr.ministore.entities.GoodsType;
import org.smr.ministore.entities.GoodsTypeOrgan;
import org.smr.ministore.entities.vo.Assign2OrgansVO;
import org.smr.ministore.service.GoodsTypeService;

import org.springframework.beans.factory.annotation.Autowired;


import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("goodsTypeServiceImpl")
@Transactional(readOnly = false)
public class GoodsTypeServiceImpl extends AbstractService<Long, GoodsType> implements GoodsTypeService {


    private static Logger logger = LoggerFactory.getLogger(GoodsTypeServiceImpl.class);


    @Autowired
    private GoodsTypeMapper mapper;

    @Autowired
    private GoodsTypeOrganMapper goodsTypeOrganMapper;

    @Autowired
    public GoodsTypeServiceImpl(GoodsTypeMapper mapper) {
        super(mapper);
    }

    public int save(GoodsType entity) {

        int effectCount = 0;
        super.saveAttatch(entity);

        entity.setTypePath(entity.getId().toString());
        if (ObjectUtils.notEqual(entity.getParentId(), null)) {
            GoodsType goodsType = this.queryById(entity.getParentId());
            String typePath = goodsType.getTypePath() + "/" + entity.getId();
            entity.setTypePath(typePath);
        }

        effectCount += mapper.insert(entity);

        GoodsTypeOrgan goodsTypeOrgan = new GoodsTypeOrgan();
        goodsTypeOrgan.setBelongToOrganId(entity.getOrganId());
        goodsTypeOrgan.setOrganId(entity.getOrganId());
        goodsTypeOrgan.setOrganPath(entity.getOrganPath());

        goodsTypeOrgan.setGoodsTypeId(entity.getId());
        IdGenUtils.saveAttatch(goodsTypeOrgan);

        effectCount += goodsTypeOrganMapper.insert(goodsTypeOrgan);

        return effectCount;
    }

    public int updateById(GoodsType id) {
        super.modifyAttach(id);
        return mapper.updateById(id);
    }

    @Transactional(readOnly = true)
    public GoodsType queryById(Long id) {
        return mapper.selectById(id);
    }

    public int deleteById(Long id) {
        return mapper.deleteById(id);
    }

    @Transactional(readOnly = true)
    public Pager<GoodsType> paginate(GoodsType entity, Long pageSize, Long pageNum) {

        Pager<GoodsType> pager = new Pager(pageSize, pageNum);
        List list = mapper.pagination(entity, pager);
        pager.setEntities(list);
        return pager;
    }

    public int batchSave(List<GoodsType> entities) {
        this.batchSaveAttatch(entities);
        return mapper.batchInsert(entities);
    }


    public int batchModify(List<GoodsType> entities) {
        this.batchModifyAttatch(entities);
        return mapper.batchUpdate(entities);
    }


    public int deleteByIdLogical(Long id) {
        return mapper.deleteByIdLogical(id);
    }

    public int removeById(GoodsType id) {
        int subGoodsTypeCount = mapper.queryChildTypeCount(id.getId(), id.getOrganId());
        // 存在下级商品类型不允许删除
        if (subGoodsTypeCount > GoodsType.ZERO) {
            return GoodsType.HAS_CHILD_NODE;
        }
        return this.deleteByIdLogical(id.getId());
    }

    public int assignToOrgans(Assign2OrgansVO assign2Organs) {
        if (Objects.isNull(assign2Organs)) {
            return ErrorCode.Persistence.NONE_EFFECT;
        }
        GoodsType goodsType = new GoodsType();
        List<GoodsTypeOrgan> goodsTypeOrgans = goodsType.assigneToOrgans(assign2Organs);
        if (CollectionUtils.isEmpty(goodsTypeOrgans)) {
            return ErrorCode.Persistence.NONE_EFFECT;
        }
        List<GoodsTypeOrgan> goodsTypeOrgansPesition = this.goodsTypeOrganMapper.queryAssignedOrgans(assign2Organs);
        List<GoodsTypeOrgan> goodsTypeOrgansWant2Save = goodsType.getGoodsTypeOrganWant2Save(goodsTypeOrgansPesition, goodsTypeOrgans);
        if (CollectionUtils.isEmpty(goodsTypeOrgansWant2Save)) {
            return ErrorCode.Persistence.NONE_EFFECT;
        }
        IdGenUtils.batchSaveAttach(goodsTypeOrgansWant2Save);
        return this.goodsTypeOrganMapper.batchInsert(goodsTypeOrgansWant2Save);
    }

    public List<GoodsType> getGoodsTypeTree(GoodsType goodsType) {

        return mapper.getGoodsTypeTree(goodsType);
    }

    @Override
    public Long getId() {
        return IdGenUtils.getNextLongId();
    }
}
